//
//  CorporateCenter.m
//  MUBATE
//
//  Created by sunbin on 2017/2/16.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "CorporateCenter.h"

#import "AuthenticationInfor.h"
#import "ChangePwd.h"

#import "YStaticCell.h"

@interface CorporateCenter ()<LCActionSheetDelegate ,UIImagePickerControllerDelegate ,UINavigationControllerDelegate>
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) UIImagePickerController *picker;

@property (weak, nonatomic) IBOutlet UITableView *tableview;
@end

@implementation CorporateCenter
{
    YUserInfo   *userInfor;
    UIImageView *user_img;
    NSArray *cellTextAry;NSArray *detailTextLabelAry;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = LocalizedString(@"Corporate Center");
    self.isNoShowNoNetError = YES;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    userInfor = [YConfig myProfile];
    
    
    [[UMSocial sharedUMSocial] shareTy:SceneSina fromeCtl:self sendLinkURL:nil TagName:nil title:@"title" description:@"description" thumbImage:Icon(@"")];
    
    // 本地数据
    cellTextAry =
  @[
  @[LocalizedString(@"Avatar") ,LocalizedString(@"Company Name") ,LocalizedString(@"Branch") ,LocalizedString(@"Region")] ,
  @[LocalizedString(@"Enterprise Authentication") ,LocalizedString(@"Change Password") ,LocalizedString(@"Modify phone number") ,LocalizedString(@"Modify email address")]
  ];
    detailTextLabelAry =
  @[
  @[userInfor.company_name ,userInfor.store_name ,[CommonUtils getInforArea:userInfor]],
  @[userInfor.store_phone ,userInfor.store_email]
  ];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return cellTextAry.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[cellTextAry objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    static NSString *cellid = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellid];
        cell.textLabel.font = textLabelFont;
        cell.detailTextLabel.font = detailTextLabelFont;
        // 禁止点击 跳转
        if (indexPath.section == 0) {
            cell.accessoryType  = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    }
    
    cell.textLabel.text = [[cellTextAry objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    
    cell.textLabel.textColor = [UIColor businessCenterCellTextColor];
    cell.detailTextLabel.textColor = [UIColor businessCenterCellTextColor];
    cell.textLabel.font = businessCenterCellTextFont;
    cell.detailTextLabel.font = businessCenterCellDetailTextLabelFont;

    
    
    if ((indexPath.row !=0 && indexPath.section !=1) || (indexPath.row >=2 && indexPath.section !=0)) {
        cell.detailTextLabel.text = [[detailTextLabelAry objectAtIndex:indexPath.section] objectAtIndex:(indexPath.row !=0 && indexPath.section !=1)?indexPath.row-1:indexPath.row-2];
    }else{
        if (indexPath.row == 0 && indexPath.section == 0) {
            // 显示用户图片
            cell.accessoryView = (UIImageView *)[self YMprofile:HEIGHT(cell)];
//            cell.accessoryView.fl_badgeValue = @"1";
        }
    }
    
    // 设置点击 禁止点击
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 1) {
        // 跳转->指定控制器

        NSArray *ctlAry = @[kVCFromSb(@"AuthenticationInforID", @"CorporateCenter"),
                            kVCFromSb(@"ChangePwdID"          , @"CorporateCenter"),
                            kVCFromSb(@"ModifyID"             , @"CorporateCenter"),
                            kVCFromSb(@"ModifyID"             , @"CorporateCenter")];
        
        YStaticCell *staicCtl = [ctlAry objectAtIndex:indexPath.row];
        staicCtl.userInfor = userInfor;
        if (indexPath.row >1) {
            staicCtl.bingAccountType = indexPath.row-2;
        }
        [self.navigationController pushViewController:staicCtl animated:NO];

    }else{
        if (indexPath.row == 0 && indexPath.section == 0) {
            LCActionSheet *actionSheet = [LCActionSheet sheetWithTitle:nil buttonTitles:@[LocalizedString(@"Photograph") ,LocalizedString(@"album")] redButtonIndex:-1 delegate:self];
            [actionSheet show];
        }}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return kscaleHeight(70.0f);
    }else{
        return kscaleHeight(50.0f);
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kscaleHeight(10.0f);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return kscaleHeight(0.1f);
}


// 显示用户头像
- (UIImageView*)YMprofile:(CGFloat)ROW_H{
    user_img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2.5, kscaleHeight(50.f), kscaleHeight(50.f))];    
    [user_img mac_setImageWithURL:ImageAdress(userInfor.store_img) placeholderImage:Icon(@"touxiang")];
    return user_img;
}

/**
 *  点击了 buttonIndex处 的按钮 0-上下关系
 */
- (void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%ld" ,buttonIndex);
     // 权限
    [NHUtils imagePickerPermissions:self didClickedButtonAtIndex:buttonIndex];
}


#pragma make UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    WEAKSELF;
    _image =  info[UIImagePickerControllerEditedImage];
    
    [picker dismissViewControllerAnimated:YES completion:^ {
        [weakSelf updatePortrait:@[_image]];
    }];
}
// 刷新图片 加载
- (void)updatePortrait:(NSArray *)images{
    WEAKSELF;
    //模拟 圈圈
    [MBProgressHUD showMessage:nil];

    [NHUtils updatePortrait:images name:@"imgsrc" mimeType:@"image/png" upDataSucceed:^(NSString *msg, NSString *link) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];

            userInfor.store_img   = link;
            kAppDelegate.userInfo = userInfor;
            [weakSelf.tableview reloadData];
            [weakSelf.view showSuccess:msg];
        });

        
    } upDataFail:^(NSString *msg) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];

            [weakSelf.view showError:msg];

        });

    }];









}



- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
}



@end
